package com.young.cms.model.gen4;

import com.young.cms.model.Column;
import com.young.cms.model.enums.EAsConditionTypeEnum;
import com.young.cms.model.enums.EColumnTypeEnum;
import com.young.cms.model.enums.EControlTypeEnum;
import com.young.cms.model.enums.EShowFormatEnum;
import com.young.common.model.BaseDO;
import com.young.common.model.enums.EYesOrNoEnum;
import com.young.common.util.StringUtils;


/**
 * 代码生成字段配置实体类
 * Created by rookie on 2018-7-12.
 */
public class GenColV4 {

    private String id;//主键

    private String genId;//代码生成编号,关联tb_cms_gen_main

    private String fieldName;//字段名

    private String attrName;//java属性名

    private String fieldRemark;//字段描述

    private String fieldType;//字段类型

    private Integer fieldLength;//字段长度

    private String fieldNotNull;//字段非空

    private String viewShow;//是否在视图显示

    private Integer viewNum;//在视图中的顺序

    private String asCondition;//是否作为查询条件

    private String supportSort;//是否支持排序


    private String formShow;//在表单显示方式

    private Integer formNum;//在表单中的顺序

    private String controlType;//控件类型,input/select..

    private String controlMust;//是否必填

    private String controlParam;//控件参数,例如url,取值等

    private String controlDefault;//默认值

    private String controlReadonly;//只读属性

    private String controlClass;//控件样式

    private String controlCheck;//控件表单验证

    private String controlCheckVal;//控件表单验证值

    private String fillExplain;//填写说明


    // ===== 代码生成V4新增字段 start =====

    // 字段java类型
    private String javaType;
    // 支持导入
    private String supportImport;
    // 支持导出
    private String supportExport;

    // 表头标题
    private String headerTitle;
    // 表头align, left,center,right
    private String headerAlign;
    // 内容align, left,center,right
    private String valueAlign;
    // 固定列, left,no,right
    private String columnFixed;
    // 表头提示
    private String headerTips;
    // 列宽
    private String columnWidth;
    // 视图格式
    private String viewValueFormat;

    // 表单控件宽度
    private Integer controlWidth;
    // 控件配置
    private ControlConfig controlConfig;
    // 显示控制
    private ColShowConfig showConfig;
    // 控件标题
    private String controlTitle;
    // 控件占位符提示
    private String controlPlaceholder;


    // ===== 代码生成V4新增字段 end =====

    public GenColV4() {
        super();
    }

    public static GenColV4 buildDefaultV4(Column column) {
        GenColV4 genColV4 = new GenColV4();
//        genCol.setId();
//        genCol.setGenId();
        genColV4.setFieldName(column.getField());
        genColV4.setAttrName(StringUtils.toCamel(column.getField()));
        genColV4.setFieldRemark(column.getRemark());
        genColV4.setFieldType(column.getType());
        genColV4.setFieldLength(column.getLength());
        genColV4.setFieldNotNull(column.getNotNull());
        // 字段类型
        EColumnTypeEnum columnTypeEnum = EColumnTypeEnum.ofOrDefault(column.getType());
        // 字段
        genColV4.setJavaType(columnTypeEnum != null ? columnTypeEnum.getJavaType() : "String");

        // 填充视图属性
        fillViewDefault(genColV4, column);

        // 填充表单属性
        fillFormDefault(genColV4, column);

        return genColV4;
    }

    /**
     * 填充视图属性
     * @param genColV4
     * @param column
     */
    private static void fillViewDefault(GenColV4 genColV4, Column column) {
        // 字段类型
        EColumnTypeEnum columnTypeEnum = EColumnTypeEnum.ofOrDefault(column.getType());
        // 视图
        if ("text".equalsIgnoreCase(column.getType())
                || (column.getLength() != null && column.getLength() > 2000)) {
            genColV4.setViewShow(EYesOrNoEnum.NO.getCode().toString());
            genColV4.setSupportImport(EYesOrNoEnum.NO.getCode().toString());
            genColV4.setSupportExport(EYesOrNoEnum.NO.getCode().toString());
        } else {
            genColV4.setViewShow(EYesOrNoEnum.YES.getCode().toString());
            genColV4.setSupportImport(EYesOrNoEnum.YES.getCode().toString());
            genColV4.setSupportExport(EYesOrNoEnum.YES.getCode().toString());
        }
        genColV4.setViewNum(9000);

        if (columnTypeEnum != null && (columnTypeEnum.isNumberType() || columnTypeEnum.isDateType())) {
            genColV4.setSupportSort(EYesOrNoEnum.YES.getCode().toString());
        } else {
            genColV4.setSupportSort(EYesOrNoEnum.NO.getCode().toString());
        }
        if (column.getField().toLowerCase().endsWith("id")) {
            genColV4.setAsCondition(EAsConditionTypeEnum.EQ.getType());
        } else if (column.getField().toLowerCase().endsWith("name")) {
            genColV4.setAsCondition(EAsConditionTypeEnum.LIKE.getType());
        } else if (column.getField().toLowerCase().endsWith("time")) {
            genColV4.setAsCondition(EAsConditionTypeEnum.BETWEEN_AND.getType());
        } else {
            genColV4.setAsCondition(EAsConditionTypeEnum.NO.getType());
        }
        if (column.getRemark().split(",").length > 1) {
            genColV4.setHeaderTitle(column.getRemark().split(",")[0]);
        } else {
            genColV4.setHeaderTitle(column.getRemark());
        }
        genColV4.setHeaderAlign("left");
        genColV4.setValueAlign("left");
        genColV4.setColumnFixed("no");
        genColV4.setHeaderTips("");
        genColV4.setColumnWidth("100");
        genColV4.setViewValueFormat(EShowFormatEnum.TEXT.getType());
    }

    /**
     * 填充表单属性
     * @param genColV4
     * @param column
     */
    private static void fillFormDefault(GenColV4 genColV4, Column column) {
        // 表单
        genColV4.setFormShow(EYesOrNoEnum.YES.getCode().toString());
        genColV4.setFormNum(9000);
        EColumnTypeEnum columnTypeEnum = EColumnTypeEnum.ofOrDefault(column.getType());
        if (columnTypeEnum.isDateType()) {
            genColV4.setControlType(EControlTypeEnum.DATE.getType());//控件类型
        } else if (EColumnTypeEnum.TEXT == columnTypeEnum) {
            genColV4.setControlType(EControlTypeEnum.TEXTAREA.getType());//控件类型
        } else if (EColumnTypeEnum.VARCHAR == columnTypeEnum && column.getLength() != null && column.getLength() > 2000) {
            genColV4.setControlType(EControlTypeEnum.TEXTAREA.getType());//控件类型
        } else {
            genColV4.setControlType(EControlTypeEnum.INPUT.getType());//控件类型
        }

        if (EYesOrNoEnum.YES.getCode().toString().equals(column.getNotNull())) {
            genColV4.setControlMust(EYesOrNoEnum.YES.getCode().toString());
        } else {
            genColV4.setControlMust(EYesOrNoEnum.NO.getCode().toString());
        }
//        genCol.setControlParam();
//        genCol.setControlDefault();
        genColV4.setControlReadonly("none");
        genColV4.setControlClass("2");
//        genCol.setControlCheck();
//        genCol.setControlCheckVal();
//        genCol.setFillExplain();

        genColV4.setControlWidth(12);
        if (column.getRemark().split(",").length > 1) {
            genColV4.setControlTitle(column.getRemark().split(",")[0]);
            genColV4.setControlPlaceholder(column.getRemark().split(",")[0]);
        } else {
            genColV4.setControlTitle(column.getRemark());
            genColV4.setControlPlaceholder(column.getRemark());
        }
        genColV4.setControlConfig(ControlConfig.buildDefault(genColV4));
        genColV4.setShowConfig(ColShowConfig.buildDefault());
    }

    /**
     * 判断是否BaseDO中已有的字段
     *
     * @return
     */
    public boolean isBaseDOField() {
        for (String field : BaseDO.allField()) {
            if (field.equals(this.attrName)) {
                return true;
            }
        }
        return false;
    }

//    /**
//     * 将字段类型转换为对应的java类型
//     */
//    public void convertFieldJavaType() {
//        if (this.getFieldType().indexOf("char") > -1 || this.getFieldType().indexOf("CHAR") > -1
//                || this.getFieldType().indexOf("text") > -1 || this.getFieldType().indexOf("TEXT") > -1) {
//            this.setFieldType("String");
//        } else if (this.getFieldType().indexOf("date") > -1 || this.getFieldType().indexOf("DATE") > -1
//                || this.getFieldType().indexOf("time") > -1 || this.getFieldType().indexOf("TIME") > -1) {
//            this.setFieldType("Date");
//        } else if ("bigint".equalsIgnoreCase(this.getFieldType())) {
//            this.setFieldType("Long");
//        } else if (this.getFieldType().indexOf("number") > -1 || this.getFieldType().indexOf("NUMBER") > -1
//                || this.getFieldType().indexOf("int") > -1 || this.getFieldType().indexOf("INT") > -1) {
//            this.setFieldType("Integer");
//        } else if (this.getFieldType().indexOf("float") > -1 || this.getFieldType().indexOf("FLOAT") > -1) {
//            this.setFieldType("Float");
//        } else if (this.getFieldType().indexOf("double") > -1 || this.getFieldType().indexOf("DOUBLE") > -1) {
//            this.setFieldType("Double");
//        } else {
//            this.setFieldType("String");
//        }
//    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getGenId() {
        return genId;
    }

    public void setGenId(String genId) {
        this.genId = genId;
    }

    public String getFieldName() {
        return fieldName;
    }

    public void setFieldName(String fieldName) {
        this.fieldName = fieldName;
    }

    public String getAttrName() {
        return attrName;
    }

    public void setAttrName(String attrName) {
        this.attrName = attrName;
    }

    public String getFieldRemark() {
        return fieldRemark;
    }

    public void setFieldRemark(String fieldRemark) {
        this.fieldRemark = fieldRemark;
    }

    public String getFieldType() {
        return fieldType;
    }

    public void setFieldType(String fieldType) {
        this.fieldType = fieldType;
    }

    public Integer getFieldLength() {
        return fieldLength;
    }

    public void setFieldLength(Integer fieldLength) {
        this.fieldLength = fieldLength;
    }

    public String getFieldNotNull() {
        return fieldNotNull;
    }

    public void setFieldNotNull(String fieldNotNull) {
        this.fieldNotNull = fieldNotNull;
    }

    public String getViewShow() {
        return viewShow;
    }

    public void setViewShow(String viewShow) {
        this.viewShow = viewShow;
    }

    public Integer getViewNum() {
        return viewNum;
    }

    public void setViewNum(Integer viewNum) {
        this.viewNum = viewNum;
    }

    public String getAsCondition() {
        return asCondition;
    }

    public void setAsCondition(String asCondition) {
        this.asCondition = asCondition;
    }

    public String getSupportSort() {
        return supportSort;
    }

    public void setSupportSort(String supportSort) {
        this.supportSort = supportSort;
    }

    public String getFormShow() {
        return formShow;
    }

    public void setFormShow(String formShow) {
        this.formShow = formShow;
    }

    public Integer getFormNum() {
        return formNum;
    }

    public void setFormNum(Integer formNum) {
        this.formNum = formNum;
    }

    public String getControlType() {
        return controlType;
    }

    public void setControlType(String controlType) {
        this.controlType = controlType;
    }

    public String getControlMust() {
        return controlMust;
    }

    public void setControlMust(String controlMust) {
        this.controlMust = controlMust;
    }

    public String getControlParam() {
        return controlParam;
    }

    public void setControlParam(String controlParam) {
        this.controlParam = controlParam;
    }

    public String getControlDefault() {
        return controlDefault;
    }

    public void setControlDefault(String controlDefault) {
        this.controlDefault = controlDefault;
    }

    public String getControlReadonly() {
        return controlReadonly;
    }

    public void setControlReadonly(String controlReadonly) {
        this.controlReadonly = controlReadonly;
    }

    public String getControlClass() {
        return controlClass;
    }

    public void setControlClass(String controlClass) {
        this.controlClass = controlClass;
    }

    public String getControlCheck() {
        return controlCheck;
    }

    public void setControlCheck(String controlCheck) {
        this.controlCheck = controlCheck;
    }

    public String getControlCheckVal() {
        return controlCheckVal;
    }

    public void setControlCheckVal(String controlCheckVal) {
        this.controlCheckVal = controlCheckVal;
    }

    public String getFillExplain() {
        return fillExplain;
    }

    public void setFillExplain(String fillExplain) {
        this.fillExplain = fillExplain;
    }

    public String getHeaderTitle() {
        return headerTitle;
    }

    public void setHeaderTitle(String headerTitle) {
        this.headerTitle = headerTitle;
    }

    public String getHeaderAlign() {
        return headerAlign;
    }

    public void setHeaderAlign(String headerAlign) {
        this.headerAlign = headerAlign;
    }

    public String getValueAlign() {
        return valueAlign;
    }

    public void setValueAlign(String valueAlign) {
        this.valueAlign = valueAlign;
    }

    public String getColumnFixed() {
        return columnFixed;
    }

    public void setColumnFixed(String columnFixed) {
        this.columnFixed = columnFixed;
    }

    public String getHeaderTips() {
        return headerTips;
    }

    public void setHeaderTips(String headerTips) {
        this.headerTips = headerTips;
    }

    public String getColumnWidth() {
        return columnWidth;
    }

    public void setColumnWidth(String columnWidth) {
        this.columnWidth = columnWidth;
    }

    public String getViewValueFormat() {
        return viewValueFormat;
    }

    public void setViewValueFormat(String viewValueFormat) {
        this.viewValueFormat = viewValueFormat;
    }

    public String getJavaType() {
        return javaType;
    }

    public void setJavaType(String javaType) {
        this.javaType = javaType;
    }

    public String getSupportImport() {
        return supportImport;
    }

    public void setSupportImport(String supportImport) {
        this.supportImport = supportImport;
    }

    public String getSupportExport() {
        return supportExport;
    }

    public void setSupportExport(String supportExport) {
        this.supportExport = supportExport;
    }

    public Integer getControlWidth() {
        return controlWidth;
    }

    public void setControlWidth(Integer controlWidth) {
        this.controlWidth = controlWidth;
    }

    public ControlConfig getControlConfig() {
        return controlConfig;
    }

    public void setControlConfig(ControlConfig controlConfig) {
        this.controlConfig = controlConfig;
    }

    public ColShowConfig getShowConfig() {
        return showConfig;
    }

    public void setShowConfig(ColShowConfig showConfig) {
        this.showConfig = showConfig;
    }

    public String getControlTitle() {
        return controlTitle;
    }

    public void setControlTitle(String controlTitle) {
        this.controlTitle = controlTitle;
    }

    public String getControlPlaceholder() {
        return controlPlaceholder;
    }

    public void setControlPlaceholder(String controlPlaceholder) {
        this.controlPlaceholder = controlPlaceholder;
    }
}